<template>
    <div>
        <div>
            <el-input size="small" class="addPosInput" placeholder="添加职位" v-model="pos.name" clearable></el-input>
            <el-button size="small" icon="el-icon-plus" type="primary" @click="addPosition">添加</el-button>
            <el-button type="danger" size="small"
                       :disabled="this.multipleSelction.length == 0"
                       @click="deleteMany">批量删除</el-button>
        </div>
        <div class="posManaMain">
            <el-table stripe border
                      :data="positions"
                      @selection-change="handleSelectionChange"
                      style="width: 100%" size="small">
                <el-table-column type="selection" width="55"></el-table-column>
                <el-table-column prop="id" label="编号" width="55"></el-table-column>
                <el-table-column prop="name" label="职位" width="120"></el-table-column>
                <el-table-column prop="createdate" label="创建时间" width="200"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini"
                                   @click="showEditView(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger"
                                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <el-dialog title="编辑职位" :visible.sync="dialogVisible" width="30%">
            <div class="updatePosInput">
                <span><b>职位名称:</b></span>
                <el-input v-model="updatePos.name" size="small" ></el-input>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdate">保 存</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "PosMana",
        data() {
            return{
                pos:{
                    name: ''
                },
                updatePos:{
                    name: ''
                },
                positions: [],
                multipleSelction: [],
                dialogVisible: false
            }
        },
        mounted() {
            this.initPositions();
        },
        methods:{
            //初始化
            initPositions(){
                this.getRequest('/system/basic/pos/').then(resp => {
                    if (resp){
                        this.positions = resp;
                        this.pos.name = '';
                    }
                })
            },
            //添加
            addPosition(){
                if (this.pos.name){
                    this.postRequest('/system/basic/pos/',this.pos).then(resp => {
                        if (resp){
                            this.initPositions();
                        }
                    })
                }else{
                    this.$message.error('职位名称不能为空')
                }
            },
            //编辑
            showEditView(index,data){
                Object.assign(this.updatePos,data);
                this.updatePos.createDate ='';
                this.dialogVisible = true;
            },
            //删除
            handleDelete(index,data){
                this.$confirm('此操作将永久删除['+ data.name +']职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/pos/' + data.id).then(resp =>{
                        if (resp){
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //批量删除
            deleteMany(){
                this.$confirm('此操作将永久删除['+ this.multipleSelction.length +']条职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids = '?';
                    this.multipleSelction.forEach(item => {
                        ids += 'ids=' + item.id + '&';
                    });
                    this.deleteRequest('/system/basic/pos/' + ids).then(resp =>{
                        if (resp){
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //保存按钮
            doUpdate(){
                this.putRequest('/system/basic/pos/',this.updatePos).then(resp => {
                    if (resp){
                        this.initPositions();
                        this.dialogVisible = false;
                    }
                });
            },
            //多选设置-批量删除开关
            handleSelectionChange(val){
                this.multipleSelction = val;
            }
        }
    }
</script>

<style>
    .addPosInput{
        width: 200px;
        margin-right: 5px;
    }
    .posManaMain{
        margin-top: 10px;
    }

    .updatePosInput .el-input{
        width: 200px;
        margin-left: 8px;
    }
</style>